package com.song.util.excel;

import cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

public class CustomExcelExportStyler extends AbstractExcelExportStyler implements IExcelExportStyler {
    public CustomExcelExportStyler(){}

    public CustomExcelExportStyler(Workbook workbook){
        super.createStyles(workbook);
    }

    @Override
    public CellStyle getHeaderStyle(short i) {
        Font headerFont = workbook.createFont();
        //字体样式
        headerFont.setFontName("宋体");
        //字体颜色
        headerFont.setColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        //字体加粗
        headerFont.setBold(true);
        //字体大小
        headerFont.setFontHeightInPoints((short) 14);
        CellStyle headerStyle = createBaseStyle(workbook);
        headerStyle.setFont(headerFont);
        //自动换行
        headerStyle.setWrapText(true);
        //填充背景颜色
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headerStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
        return headerStyle;
    }

    @Override
    public CellStyle getTitleStyle(short i) {
        Font titleFont = workbook.createFont();
        //字体样式
        titleFont.setFontName("宋体");
        //字体颜色
        titleFont.setColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        //字体加粗
        titleFont.setBold(true);
        titleFont.setFontHeightInPoints((short) 12);
        CellStyle titleStyle = createBaseStyle(workbook);
        titleStyle.setFont(titleFont);
        //自动换行
        titleStyle.setWrapText(true);
        //填充背景颜色
        titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        titleStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
        return titleStyle;
    }

    @Override
    public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);
        CellStyle cellStyle = createBaseStyle(workbook);
        cellStyle.setFont(font);
        cellStyle.setDataFormat(STRING_FORMAT);
        cellStyle.setWrapText(isWarp);
        return cellStyle;
    }

    @Override
    public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);
        CellStyle cellStyle = createBaseStyle(workbook);
        cellStyle.setFont(font);
        cellStyle.setDataFormat(STRING_FORMAT);
        cellStyle.setWrapText(isWarp);
        return cellStyle;
    }

    public CellStyle createBaseStyle(Workbook workbook){
        CellStyle cellStyle = workbook.createCellStyle();
        //水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //垂直居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //上边框
        cellStyle.setBorderTop(BorderStyle.THIN);
        //下边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        //左边框
        cellStyle.setBorderLeft(BorderStyle.THIN);
        //右边框
        cellStyle.setBorderRight(BorderStyle.THIN);
        return cellStyle;
    }
}
